package com.cdream.basic.algorithm.leetcode.stack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author cdream
 * @date 6/26/21
 */
public class Question102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();

        list.add(root);
        while (!list.isEmpty()){
            List<Integer> k = new ArrayList<>();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                TreeNode t = list.poll();
                k.add(t.val);
                if (t.left!=null){
                    list.offer(t.left);
                }
                if (t.right!=null){
                    list.offer(t.right);
                }
            }
            res.add(k);
        }
        return res;
    }
}
